<script setup>
import {
  Edit,
  Delete, User
} from '@element-plus/icons-vue'
import { ref } from 'vue'
const supplier = ref([
  {   "id":"7",
    "supplierName":"钱大妈",
    "supplierAddress":"丽萨天下无敌萨迦天地总歇会",
    "supplierPhone":"1234678",
    "supplierEmail":"3526279811@qq.com",
    "postalCode":"Sc122323",
    "contactPerson":"我当时",
    "contactPhone":"21312412"
    ,"bankName":"天地银行",
    "bankAccount":"1312315",
    "status":"可用"
  }

])
const products = ref([
  {   "id":7,
    "productName":"钱大妈",
    "supplierId":1,
    "supplierName":'',
    "productDescription":"乐高",
    "sellingPrice":12,
    "productQuantity":120,
    "productSpecification":"12个",
    "packagingUnit":"个",
    "batchNumber":213412,
    "approvalNumber":"213122",
    "status":"可用"
  }

])

const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(3)//每页条数
const onSizeChange = (size) => {
  pageSize.value = size
  productList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  productList()
}
import {supplierListService}from '@/api/supplier.js'
import {productListService,deleteProductService,updateProductService,AddProductService} from "@/api/product.js";
const ProductName = ref('')
const supplierId=ref('')
const batchNumber=ref('')
const approvalNumber=ref('')
const supplierList = async ()=>{
  let  params1 = {
    pageNum: 1,
    pageSize:100,
  }
  let result =  await  supplierListService(params1);
  total.value =  result.data.total
  supplier.value = result.data.items;
}
supplierList();
const suId = ref('')
const productList = async ()=>{
  let  params = {
    pageNum: pageNum.value,
    pageSize:pageSize.value,
    ProductName:ProductName.value?ProductName.value:null,
    supplierId:supplierId.value?supplierId.value:null,
    batchNumber:batchNumber.value?batchNumber.value:null,
    approvalNumber:approvalNumber.value?approvalNumber.value:null,
  }
  let result =  await  productListService(params);
  total.value =  result.data.total
  products.value = result.data.items;
  for (let i = 0; i < products.value.length; i++) {
    let product = products.value[i]
    for (let j = 0; j < supplier.value.length; j++) {
      if (product.supplierId === supplier.value[j].id) {
        product.supplierName = supplier.value[j].supplierName
      }
    }
  }
}
productList();
import {Plus} from '@element-plus/icons-vue'
import {ElMessage, ElMessageBox} from "element-plus";
import {useTokenStore} from "@/Stores/token.js";
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const productModel = ref({
  id:'',
  productName: '',
  supplierId: '',
  productDescription: '',
  sellingPrice:'',
  productQuantity:'',
  productSpecification:'',
  packagingUnit:'',
  batchNumber:'',
  approvalNumber:'',
  status:''
})
const title =ref('')
const tokenStore = useTokenStore()
const updateProductEcho =  (row)=>{
  title.value = '修改商品'
  visibleDrawer.value = true
  productModel.value.id = row.id
  productModel.value.productName = row.productName
  productModel.value.supplierId = row.supplierId
  productModel.value.productDescription = row.productDescription
  productModel.value.sellingPrice = row.sellingPrice
  productModel.value.productQuantity = row.productQuantity
  productModel.value.productSpecification = row.productSpecification
  productModel.value.packagingUnit = row.packagingUnit
  productModel.value.batchNumber = row.batchNumber
  productModel.value.approvalNumber = row.approvalNumber
  productModel.value.status = row.status
}
const updateProduct = async (state)=>{
  productModel.value.status = state
  let result =  await updateProductService(productModel.value);
  ElMessage.success(result.data?result.data:'修改成功')
  productList()
  visibleDrawer.value = false
}
const clearData = ()=>{
  productModel.value.id = '';
  productModel.value.productName = '';
  productModel.value.supplierId = '';
  productModel.value.productDescription = '';
  productModel.value.sellingPrice = '';
  productModel.value.productQuantity = '';
  productModel.value.productSpecification = '';
  productModel.value.packagingUnit = '';
  productModel.value.batchNumber = '';
  productModel.value.approvalNumber = '';
  productModel.value.status = '';
}
const addProduct = async (state) => {
  productModel.value.status = state
  let result = await AddProductService(productModel.value)
  ElMessage.success(result.data?result.data:'添加成功')
  productList()
  visibleDrawer.value = false
}
const deleteProduct = (row) => {
  ElMessageBox.confirm(
      '确认删除该条产品吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        let result = await deleteProductService(row.id);
        ElMessage({
          type: 'success',
          message: '确认删除',
        })
        productList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消了删除',
        })
      })
}
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>商品列表</span>
        <div class="extra">
          <el-button type="primary" @click="visibleDrawer=true;title='添加商品';clearData()">添加商品</el-button>
        </div>
      </div>
    </template>
    <el-form inline>
      <el-form-item label="供应商名称:">
        <el-select placeholder="请选择供应商名称" v-model="supplierId" style="width: 180px">
          <el-option
              v-for="c in supplier"
              :key="c.id"
              :label="c.supplierName"
              :value="c.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input  placeholder="请输入供应商名" v-model="ProductName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="productList">搜索</el-button>
        <el-button @click="supplierId='';ProductName='';approvalNumber='';batchNumber='';productList()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="products" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="图片">
        <template #default="scope">
          <img src="/images/00.jpg" alt="" style="width: 100%; height: auto; display: block;">
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="productName"></el-table-column>
      <el-table-column label="供应商名称" prop="supplierName"></el-table-column>
      <el-table-column label="商品描述" prop="productDescription"></el-table-column>
      <el-table-column label="出售价格" prop="sellingPrice"></el-table-column>
      <el-table-column label="库存数量" prop="productQuantity"></el-table-column>
      <el-table-column label="商品规格" prop="productSpecification"></el-table-column>
      <el-table-column label="包装单位" prop="packagingUnit"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="updateProductEcho(row)" ></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteProduct(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />

    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
      <!-- 添加文章表单 -->
      <el-form :model="productModel" label-width="100px" >
        <el-form-item label="商品名称" >
          <el-input v-model="productModel.productName" placeholder="请输入商品名"></el-input>
        </el-form-item>
        <el-form-item label="供应商名称" >
          <el-select placeholder="请选择供应商名称" v-model="productModel.supplierId" style="width: 180px">
            <el-option
                v-for="c in supplier"
                :key="c.id"
                :label="c.supplierName"
                :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品描述" >
          <el-input v-model="productModel.productDescription" placeholder="请输入商品描述"></el-input>
        </el-form-item>
        <el-form-item label="销售价格" >
          <el-input v-model="productModel.sellingPrice" placeholder="请输入销售价格"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" >
          <el-input v-model="productModel.productQuantity" placeholder="请输入商品数量"></el-input>
        </el-form-item>
        <el-form-item label="商品规格" >
          <el-input v-model="productModel.productSpecification" placeholder="请输入商品规格"></el-input>
        </el-form-item>
        <el-form-item label="包装单位" >
          <el-input v-model="productModel.packagingUnit" placeholder="请输入包装单位"></el-input>
        </el-form-item>
        <el-form-item label="生产批号" >
          <el-input v-model="productModel.batchNumber" placeholder="请输入生产批号"></el-input>
        </el-form-item>
        <el-form-item label="批准文号" >
          <el-input v-model="productModel.approvalNumber" placeholder="请输入批准文号"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="title==='添加商品'?addProduct('可用'):updateProduct('可用')">可用</el-button>
          <el-button type="info" @click="title==='添加商品'?addProduct('不可用'):updateProduct('不可用')">不可用</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>
</template>

<style lang="scss" scoped>
.page-container {
  margin-left: 5px;
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
